﻿using Lgc_VoteSys.Models;
using Lgc_VoteSys.Models.Database;
using Lgc_VoteSys.Models.Users;
using Lgc_VoteSys.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using static Microsoft.AspNetCore.Razor.Language.TagHelperMetadata;

namespace Lgc_VoteSys.Controllers
{
    /// <summary>
    /// 用户模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        // 定义数据库上下文
        private readonly Lgc_VoteSysContext _db;
        private readonly IJWTService _jwtService;

        /// <summary>
        /// 构造方法 构造注入
        /// 把数据库上下文初始化
        /// </summary>
        /// <param name="db"></param>
        public UsersController(Lgc_VoteSysContext db, IJWTService jwtService)
        {
            _db = db;
            _jwtService = jwtService;
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="user">依次输入邮箱、昵称、密码、用户类型：1普通用户 2企业用户</param>
        /// <returns></returns>
        [HttpPost]
        public string Register(Users_Register user)
        {
            if (user.UEmail == null || user.UPassword == null)
            {
                return "不允许存在空值！";
            }
            if (user.UType != 0 && user.UType != 1)
            {
                return "请检查用户类型";
            }

            //生成一个盐值
            var salt = OpenPublic.CreateSalt();
            //将盐值和密码拼接
            var saltPwd = salt + user.UPassword;
            //将拼接后的密码进行MD5加密
            var md5pwd = OpenPublic.MD5Encrypt32(saltPwd);
            var newUser = new User
            {
                UEmail = user.UEmail,
                UName = user.UName,
                UPassword = md5pwd,
                USalt = salt,
                UCreateTime = System.DateTime.Now,
                UType = user.UType
            };

            var users = _db.Users.Find(user.UEmail);
            if (users != null)
            {
                return "该邮箱已存在";
            }
            _db.Users.Add(newUser);
            return _db.SaveChanges() > 0 ? "注册成功" : "注册失败";
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="user">依次输入邮箱、密码</param>
        /// <returns></returns>
        [HttpPost]
        public string Login(Users_Login user)
        {
            if (user.UEmail == null || user.UPassword == null)
            {
                return "不允许存在空值！";
            }
            
            // 读取数据库中的盐值
            var salt = _db.Users.Where(x => x.UEmail == user.UEmail).Select(x => x.USalt).FirstOrDefault();
            // 将盐值和密码拼接
            var saltPwd = salt + user.UPassword;
            // 将拼接后的密码进行MD5加密
            var md5pwd = OpenPublic.MD5Encrypt32(saltPwd);
            user.UPassword = md5pwd;
            var row = _db.Users.Count(x => x.UEmail == user.UEmail & x.UPassword == user.UPassword);
            if (row > 0)
            {
                return "您的token：" + _jwtService.CreateToken(user.UEmail);
            }
            else
            {
                return "账号或密码错误";
            }
        }
    }
}
